// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-12-07 20:32:49
 * @LastEditors: qiulin_JXGM ql1746365080@gmail.com
 * @LastEditTime: 2024-12-20 09:49:09
 * @FilePath: \SGSim\Import\BDFImport\BulkData\Dynamic\FREQ1Parser.cpp
 */

#include "FREQ1Parser.h"

#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Dynamic/IFREQ1Service.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (FREQ1);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/FREQ1/FREQ1.md>
*/
// 以下需要修改
enum FREQ1KEY : std::uint8_t
{
    FREQ1KEY_SID = 2,
    FREQ1KEY_F1  = 3,  // 复第一个频率
    FREQ1KEY_DF  = 4,  // 频率增量
    FREQ1KEY_NDF = 5,  // 频率增量的数量
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseFREQ1 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::FREQ1Data tempFREQ1;  // 创建一个临时FREQ1数据对象

    // TODO(bug 001) m_dominId的正确填写
    tempFREQ1.m_domainId = 1;  // 暂时将m_domainId设为1，待修复

    int curIDKey = 1;          // 当前字符串是第几项

    // 遍历分割后的每个词条
    for (std::string& curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空白
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前字符串为空白，则跳过并处理下一个
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        // 根据当前词条的位置进行相应的解析
        switch (curIDKey)
        {
        case FREQ1KEY_SID:  // 节点ID
            tempFREQ1.m_sId = convertToI64 (curStrKey);
            break;
        case FREQ1KEY_F1:   // 节点定义坐标系，默认为0
            tempFREQ1.m_F1 = convertToReal (curStrKey);
            break;
        case FREQ1KEY_DF:   // X坐标
            tempFREQ1.m_DF = convertToReal (curStrKey);
            break;
        case FREQ1KEY_NDF:  // Y坐标
            tempFREQ1.m_NDF = convertToI64 (curStrKey);
            break;
        default:            // 其他情况不做处理
            break;
        }

        // 处理完当前词条后，移动到下一个词条
        curIDKey++;
    }

    // 将解析后的FREQ1数据添加到数据库中
    return dbServiceFactory->get<IFREQ1Service> ()->Add (tempFREQ1);
}